package com.lsa.letcode.the101.symmetricTree;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Arrays;

import org.junit.Test;

import com.google.common.collect.Iterators;
import com.lsa.letcode.common.TreeNode;
import com.lsa.letcode.common.TreeUtils;
import com.lsa.letcode.the101.symmetricTree.Solution.Order;

public class SymmeticTreeTest {
	@Test
	public void test() throws Exception {
		TreeNode n = TreeUtils.fromTgf(this.getClass(), "sym2.tgf");
		Solution s = new Solution();

		assertEquals(Arrays.asList(5, 3, 6, 2, 4, 1, 4, 2, 6, 3, 5),
				Arrays.asList(Iterators.toArray(s.treeIter(n, Order.INORDER),
						Integer.class)));
		
		assertEquals(Arrays.asList(5, 3, 6, 2, 4, 1, 4, 2, 6, 3, 5),
				Arrays.asList(Iterators.toArray(s.treeIter(n, Order.RIGHT_VAL_LEFT),
						Integer.class)));
		
		assertTrue(s.isSymmetric(n));
	}
	
	@Test
	public void test2() throws IOException {
		test("sym.tgf", false);
		test("sym2.tgf", true);
		test("sym3.tgf", false);
		test("sym4.tgf", false);
		test("sym5.tgf", false);
	}

	private void test(String resName, boolean isSymmetricExpected)
			throws IOException {
		TreeNode n = TreeUtils.fromTgf(this.getClass(), resName);
		Solution s = new Solution();
		assertEquals(isSymmetricExpected, s.isSymmetric(n));
	}
}